package com.egoo.ticket.server.taskschedul;

import com.egoo.eeip.eeif.framework.base.util.file.FileUtil;
import com.egoo.ticket.server.config.redis.RedisKeyConstant;
import com.egoo.ticket.server.service.EripDataSynService;
import com.egoo.ticket.server.service.EurmDataSyncService;
import com.egoo.ticket.server.service.ScheduService;
import com.egoo.ticket.server.taskschedul.repeattimeoutscanningresolve.annotation.RedisLock;
import com.egoo.ticket.server.utils.common.ConstantCode;
import com.egoo.ticket.server.utils.common.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.Arrays;
import java.util.List;

/**
 * @Date: 2019/10/3 17:05
 * @Description: 定时任务执行器
 */
@Slf4j
@RestController
class SchedulExcute {

	@Autowired
	private ScheduService scheduService;
	@Autowired
	private EripDataSynService eripDataSynService;
	@Autowired
	private RedisUtil redisUtil;
	@Autowired
	private EurmDataSyncService eurmDataSyncService;

	@Value("${eurmSynData.syn-tenantIds}")
	private String synTenantIds;
	@Value("${timer.switch}")
	private boolean synSwitch;

	/**
	 * TODO 每晚00：00删除redis中工单id生成顺序号
	 * @author: victor_tang
	 * @createtime: 2020/4/17 10:23
	 * @param
	 * @return: void
	 */
	@Scheduled(cron = "0 0 0 * * ?")
	public void deleteRedisWorkOrderIdNum() {
		// 删除工单id编号
		redisUtil.del(RedisKeyConstant.WORK_ORDER_ID_NUM);
	}
	/**
	 * TODO 超时扫描
	 * @author: victor_tang
	 * @createtime: 2020/4/17 10:21
	 * @param
	 * @return: void
	 */
//	@Scheduled(cron = "0 0 0 * * ?")//每晚00点
//	@Scheduled(cron = "*/30 * * * * ?")//每隔30秒
	@Scheduled(cron = "0 */2 * * * ?")//每隔1分钟
	@RedisLock(lockPrefix = "workOrder",lockKey = "timeOut")
	public void workOrderTimeOut() {
		log.info("工单任务超时定时器开始处理...");
		scheduService.workOrderTimeOut();
	}
	/**
	 * TODO 每晚00：00删除已超时记录
	 * @author: victor_tang
	 * @createtime: 2020/4/17 10:23
	 * @param
	 * @return: void
	 */
	@Scheduled(cron = "0 0 0 * * ?")
	public void deleteAlreadyHanleTimeOutRecord() {
		// 删除已超时记录
		scheduService.deleteAlreadyHanleTimeOutRecord();
	}
	/**
	 * TODO 每晚00：00删除modelXml下无用的文件
	 * @author: victor_tang
	 * @createtime: 2020/4/17 10:23
	 * @param
	 * @return: void
	 */
	@Scheduled(cron = "0 0 0 * * ?")
	public void deleteModelXmlFiles(){
		String path = System.getProperty("user.dir") + "/modelXml";
		File file = new File(path);
		if (!file.exists()) {
			return;
		}
		// 删除临时文件夹
		FileUtil.deleteQuietly(file);
		file.mkdir();
	}
	/**
	 * TODO 定时任务同步内管用户，角色，部门数据
	 * @author: victor_tang
	 * @createtime: 2020/4/17 10:23
	 * @param
	 * @return: void
	 */
	@Scheduled(cron = "${timer.cron.userroledept}")
	public void eripDataSys() {
		if (!synSwitch) {
			return;
		}
		log.info("=================工单定时任务同步内管用户角色部门数据================");
		if (StringUtils.isEmpty(synTenantIds)) {
			log.error(synTenantIds+"需要同步用户，角色，部门信息的租户id不能为空！");
			return;
		}
		List<String> tenantIdList = Arrays.asList(synTenantIds.split(","));
		tenantIdList.forEach(v->{
			eurmDataSyncService.batchPushSynPramaToMq(v, ConstantCode.SynEurmDataType.USER);
			eurmDataSyncService.batchPushSynPramaToMq(v, ConstantCode.SynEurmDataType.ROLE);
			eurmDataSyncService.batchPushSynPramaToMq(v, ConstantCode.SynEurmDataType.DEPT_GROUP);
		});
	}
}
